﻿using System;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;

namespace FBA.Utils
{
    partial class JSON
    {
        public static string Serialize<T>(T obj)
        {
            DataContractJsonSerializerSettings st = new DataContractJsonSerializerSettings();
            st.DateTimeFormat = new System.Runtime.Serialization.DateTimeFormat("yyyy-MM-dd HH:mm:ss");
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType(), st);
            MemoryStream ms = new MemoryStream();
            serializer.WriteObject(ms, obj);
            string result = Encoding.UTF8.GetString(ms.ToArray());
            ms.Close();
            return result;
        }
        public static T Deserialize<T>(string json)
        {
            DataContractJsonSerializerSettings st = new DataContractJsonSerializerSettings();
            st.DateTimeFormat = new System.Runtime.Serialization.DateTimeFormat("yyyy-MM-dd HH:mm:ss");
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T), st);
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            T result = (T)serializer.ReadObject(ms);
            ms.Close();
            return result;
        }
    }
}
